package com.work.dyduser.module.pagemine;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.work.dyduser.R;
import com.work.dyduser.base.BaseActivity;
import com.work.dyduser.bean.UserMessage;
import com.work.dyduser.cache.Ckey;
import com.work.dyduser.cache.SPutils;
import com.work.dyduser.httpretrofit.Glide.GlideImgManager;
import com.work.dyduser.httpretrofit.MyCallback;
import com.work.dyduser.httpretrofit.PostBody;
import com.work.dyduser.httpretrofit.RetrofitManager;
import com.work.dyduser.module.main.MainActivity;
import com.work.dyduser.utils.JsonUtils;
import com.work.dyduser.utils.UIUtils;
import com.work.dyduser.utils.VerificationUtil;
import com.work.dyduser.utils.ViewUtils;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import retrofit2.Call;


/**
 * author: Aly
 * date :2018/11/5 11:50
 * version:1.0
 * description:登陆页面
 */
public class LoginActivity extends BaseActivity {
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_password)
    EditText etPassword;
    @BindView(R.id.bt_login)
    TextView btLogin;
    @BindView(R.id.tv_forgetpassword)
    TextView tvForgetpassword;
    @BindView(R.id.tv_newuser)
    TextView tvNewuser;
    @BindView(R.id.iv_logo)
    ImageView ivLogo;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_pagemine_login;
    }

    @Override
    protected void initView() {
        mTitleBar.titleMiddle.setText("登录");
        Drawable dra= getResources().getDrawable(R.mipmap.close_error);
        dra.setBounds( 0, 0, dra.getMinimumWidth(),dra.getMinimumHeight());
        mTitleBar.titleBack.setCompoundDrawables(dra,null, null, null);
        ViewUtils.setOnClickListeners(this, btLogin, tvForgetpassword, tvNewuser);
    }

    @Override
    protected void initData() {
        GlideImgManager.glideLoader(this, R.mipmap.logo, ivLogo, 0);
    }

    @Override
    protected void processClick(View v) {
        super.processClick(v);
        switch (v.getId()) {
            case R.id.bt_login:
                //登陆
                HideKeyboard(etPhone);
                if (VerificationUtil.isPhone(etPhone.getText().toString()) && !TextUtils.isEmpty(etPassword.getText().toString())) {
                    showLoaddialog("正在登陆");
//                    login(etPhone.getText().toString(), etPassword.getText().toString());
                    UIUtils.startActivity(MainActivity.class);
                }
                break;
            case R.id.tv_forgetpassword:
                //忘记密码
//                UIUtils.startActivity(ForgetPasswordActivity.class);
                Intent intent = new Intent(this, RegisterActivity.class);
                intent.putExtra("type", 1);
                UIUtils.startActivity(intent);
                break;
            case R.id.tv_newuser:
                //新用户注册
                Intent intent1 = new Intent(this, RegisterActivity.class);
                intent1.putExtra("type", 0);
                UIUtils.startActivity(intent1);
                break;
        }
    }


    //登录
    private void login(String phone, String password) {
        Map<String, String> map = new HashMap<>();
        map.put("phone_type", "Android");
        map.put("user_secrect", password);
        map.put("user_phone", phone);
        Call<UserMessage> call = RetrofitManager.getInstance().login(PostBody.toBody(map));
        call.enqueue(new MyCallback<UserMessage>() {
            @Override
            public void onSuccess(UserMessage response) {
                if (response.getErrcode() == 0) {
                    SPutils.put(Ckey.USERMESSAGE, JsonUtils.parseBeantojson(response));
                    SPutils.put(Ckey.USERID, response.getData().getUser_id());
                    UIUtils.startActivity(MainActivity.class);
                    finish();
                } else {
                    UIUtils.showToast(response.getMsg());
                }
            }

            @Override
            public void onFailure(int errorCode, String errMsg) {
                UIUtils.showToast(errMsg);
            }

            @Override
            public void onComplete() {
                hideLoaddialog();
            }
        });

    }
}
